/**
 *
 * @author cheng sir
 * 2016年9月14日上午10:27:17
 */
package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.MD5Util;
import com.angel.dao.health.SysUserMapper;
import com.angel.health.SysUser;
import com.angel.health.SysUserExt;
import com.angel.provider.sys.SysUserProvider;

/**
 * @author cheng sir
 * 2016年9月14日上午10:27:17
 */
@Service
public class SysUserProviderImpl extends BaseProviderImpl<SysUser> implements SysUserProvider {

	@Autowired
	private SysUserMapper mapper;

	//登陆
	public Map<String, Object> login(SysUser user) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 判断用户名或密码是否为空
		if(user == null || user.getLoginName() == null || user.getLoginName() == ""){
			map.put("msg", "用户名不能为空");
			map.put("code", "0");
			return map;
		}
		user.setEmail(user.getLoginName());
		user.setMobile(user.getLoginName());
		List<SysUser> userList = mapper.accountValidate(user);
		if(userList == null || userList.size() < 1){
			map.put("msg", "用户不存在");
			map.put("code", "0");
			return map;
		}
		if(user.getPassword() == null || user.getPassword() == ""){
			map.put("msg", "密码不能为空");
			map.put("code", "1");
			return map;
		}
		//判断用户名和密码是否正确
		user.setPassword(MD5Util.getMD5(user.getPassword()));
		List<SysUser> list = mapper.login(user);
		if(list == null || list.size() < 1){
			map.put("msg", "密码有误");
			map.put("code", "1");
		}else{
			map.put("code", "2");
			map.put("msg", list.get(0));
			SysUser sysUser = list.get(0);
			sysUser.setLoginDate(new Date());
			sysUser.setLoginIp(user.getLoginIp());
			mapper.updateByPrimaryKeySelective(sysUser);
		}
		return map;
	}

	//验证用户
	public Map<String, Object> accountValidate(SysUser user, String certPic,String validatecode) {
		Map<String, Object> map = new HashMap<String, Object>();
		//判断验证码是否为空
		if(certPic == null || certPic == ""){
			map.put("msg", "验证码不能为空");
			map.put("code", "0");
			return map;
		}else if(!certPic.equals(validatecode)){
			map.put("msg", "验证码不正确");
			map.put("code", "0");
			return map;
		}else{
			//判断用户名是否为空
			if(user == null || user.getLoginName() == null || user.getLoginName() == ""){
				map.put("msg", "用户名不能为空");
				map.put("code", "1");
			}else{
				user.setEmail(user.getLoginName());
				user.setMobile(user.getLoginName());
				List<SysUser> list = mapper.accountValidate(user);
				if(list == null || list.size() < 1){
					map.put("msg", "用户不存在");
					map.put("code", "1");
				}else{
					SysUser sysUser = list.get(0);
					map.put("sysUser", sysUser);
					map.put("code", "2");
				}
			}
		}
		return map;
	}

	//验证手机
	public Map<String, Object> phoneValidate(SysUser user, String certPic,
			String sendCertPic, SysUser sysUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		//判断是否直接get方式访问
		if(sysUser == null){
			map.put("msg", "用户非正常访问");
			map.put("code", "0");
			return map;
		}
		//判断手机号是否正确
		if(user == null || user.getMobile() == null || user.getMobile() == ""){
			map.put("msg", "请输入手机号");
			map.put("code", "0");
			return map;
		}else if(!user.getMobile().equals(sysUser.getMobile())){
			map.put("msg", "您输入的手机号不正确");
			map.put("code", "0");
			return map;
		}
		//判断验证码是否正确
		if(certPic == null || certPic == ""){
			map.put("msg", "请输入手机验证码");
			map.put("code", "1");
		}else if(!certPic.equals(sendCertPic)){
			map.put("msg", "手机验证码不正确");
			map.put("code", "1");
		}else{
			map.put("code", "2");
		}
		return map;
	}

	//重置密码
	public Map<String, Object> resetValidate(SysUser user, SysUser sysUser,String repassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		//判断是否直接get方式访问
		if(sysUser == null){
			map.put("msg", "用户非正常访问");
			map.put("code", "0");
			return map;
		}
		//判断新密码是否为空
		if(user == null || user.getPassword() == null || user.getPassword() == ""){
			map.put("msg", "请输入新密码");
			map.put("code", "0");
			return map;
		}else if(!user.getPassword().equals(repassword)){
			//判断两次密码是否一致
			map.put("msg", "两次密码输入不一致");
			map.put("code", "1");
			return map;
		}else{
			sysUser.setPassword(MD5Util.getMD5(user.getPassword()));
			mapper.updateByPrimaryKeySelective(sysUser);
			map.put("code", "2");
		}
		return map;
	}

	/* (non-Javadoc)
	 * @see com.angel.provider.sys.SysUserProvider#findAllUser(com.angel.health.SysUser)
	 */
	@Override
	public List<Map<String, Object>> findAllUser(SysUser user) {
		// TODO Auto-generated method stub
		return mapper.findAllUser(user);
	}

	@Override
	public SysUserExt findUserInfo(SysUser sysUser) {
		return mapper.findUserInfo(sysUser);
	}

	@Override
	public void resetpassword(SysUser sysUser) {
		mapper.updateByPrimaryKeySelective(sysUser);
	}


	
	@Override
	public List<Map<String, Object>> findUserByPhone(SysUser user) {
		// TODO Auto-generated method stub
		SysUser su = new SysUser();
		su.setMobile(user.getMobile());
		return mapper.findAllUser(su);
	}
	

}
